/*
 * Copyright 2006 Mark Bakker <bakker.mark AT gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.gwt.vector.client.impl;

import java.util.Iterator;
import java.util.Set;

import com.google.gwt.user.client.Element;
import com.google.gwt.vector.client.SVG;
import com.google.gwt.vector.client.ui.BasicShape.Fill;
import com.google.gwt.vector.client.ui.BasicShape.Stroke;
import com.google.gwt.vector.client.ui.Polyline.Point;

/**
 * The standard SVG implementation.
 */
public class VectorImpl {

  public Element createCircle() {
    return SVG.createCircle();
  }

  public Element createEclipse() {
    return SVG.createEclipse();
  }

  public Element createFill() {
	throw new RuntimeException("createFill is only used for VML");
  }

  public Element createLine() {
    return SVG.createLine();
  }

  public Element createPolyline() {
    return SVG.createPolyline();
  }

  public Element createRectangle() {
    return SVG.createRectangle();
  }

  public Element createStroke() {
	throw new RuntimeException("createStroke is only used for VML");
  }

  public Element createVectorGroup() {
    return SVG.createVectorGroup();
  }

  public boolean isVml() {
    return false;
  }

  public void setAttribute(Element element, String key, String value) {
    SVG.setAttributeNS(element, key, value);
  }

  public void setColor(Fill fill, String color) {
    setAttribute(fill.getElement(), "color", color);
  }

  public void setColor(Element element, String color) {
    setAttribute(element, "fill", color);
  }

  public void setCoord(Element element, double coordinateX, double coordinateY, double width, double height) {
    setAttribute(element, "x", String.valueOf(coordinateX));
    setAttribute(element, "y", String.valueOf(coordinateY));
  }

  public void setDimension(Element element, double width, double height) {
    setAttribute(element, "height", String.valueOf(height));
    setAttribute(element, "width", String.valueOf(width));
  }

  public void setEclipseCoord(Element element, double coordinateX, double coordinateY, double radiusX, double radiusY) {
    setAttribute(element, "cx", String.valueOf(coordinateX));
    setAttribute(element, "cy", String.valueOf(coordinateY));
  }

  public void setFrom(Element element, double fromX, double fromY) {
    setAttribute(element, "x1", String.valueOf(fromX));
    setAttribute(element, "y1", String.valueOf(fromY));
  }

  public void setTo(Element element, double fromX, double fromY) {
    setAttribute(element, "x2", String.valueOf(fromX));
    setAttribute(element, "y2", String.valueOf(fromY));
  }

  public void setGroupDimension(Element element, double width, double height) {
    // do nothing
  }

  public void setOpacity(Fill fill, double opality) {
    setAttribute(fill.getElement(), "opacity", String.valueOf(opality));
  }

  public void setOpacity(Element element, double opality) {
    setAttribute(element, "fill-opacity", String.valueOf(opality));
  }

  public void setPoints(Element element, Set points) {
	String pointString = "";
	for (Iterator iter = points.iterator(); iter.hasNext();) {
		Point point = (Point) iter.next();
		pointString = pointString + point.getX() + "," + point.getY();
		if (iter.hasNext()) {
			pointString = pointString + " ";
		}
	}
    setAttribute(element, "points", pointString);
  }

  public void setRadius(Element element, double radius) {
    setAttribute(element, "r", String.valueOf(radius));
  }

  public void setRadius(Element element, double radiusX, double radiusY) {
    setAttribute(element, "rx", String.valueOf(radiusX));
    setAttribute(element, "ry", String.valueOf(radiusY));
  }

  public void setStrokeColor(Stroke stroke, String color) {
    setAttribute(stroke.getElement(), "color", color);
  }

  public void setStrokeColor(Element element, String color) {
    setAttribute(element, "stroke", color);
  }

  public void setStrokeOpacity(Element element, double opacity) {
    setAttribute(element, "stroke-opacity", String.valueOf(opacity));
  }

  public void setStrokeOpacity(Stroke stroke, double opality) {
    setAttribute(stroke.getElement(), "opacity", String.valueOf(opality));
  }

  public void setStrokeWidth(Element element, double width) {
    setAttribute(element, "stroke-width", String.valueOf(width));
  }

  public void setStrokeWidth(Stroke stroke, double width) {
    setAttribute(stroke.getElement(), "weight", String.valueOf(width));
  }
}
